// pages/index/houseDetail/houseDetail.js
const imageCdn = 'https://tdesign.gtimg.com/mobile/demos';
const swiperList = [{
    value: `${imageCdn}/swiper1.png`,
    ariaLabel: '图片1',
  },
  {
    value: `${imageCdn}/swiper2.png`,
    ariaLabel: '图片2',
  },
  {
    value: `${imageCdn}/swiper1.png`,
    ariaLabel: '图片1',
  },
  {
    value: `${imageCdn}/swiper2.png`,
    ariaLabel: '图片2',
  },
];
Page({


  /**
   * 页面的初始数据
   */
  data: {
    job: [],
    jobList: [],
    id: '',
    isClick: false,
    jobStorage: [],
    jobId: '',
    type1: 0,
    type: 0,
    houseId: 0,
    userId: parseInt(wx.getStorageSync('userInfo').userId, 10), // 转换为整数类型,
    fangdongname:'',
    current: 1,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList,
    longitude: 0, // 示例经度  
    latitude: 0, // 示例纬度  
    markers: [],
    house: [], //该房子信息
    home: [], //该房东所有房子信息
    // houseId: 0,
    // userId: 0,
    collection: false
  },
  markerTap: function (e) {
    // 处理点击标记点的逻辑  
    // console.log('Marker tapped', e.detail.markerId);
  },
  Tobooking: function (e) {
    console.log("one")
    console.log(this.data.houseId)
    console.log("two")
    wx.request({
      url:'http://localhost:8080/biz/appoint/list',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data:{
        houseId:this.data.houseId,
        userId:wx.getStorageSync('userInfo').userId
      },
      success:(resp)=>{
        //console.log(resp.data)
        if(resp.data.rows!=0){
          wx.showToast({
            title: '你已预约',
          })
        }else{
          wx.navigateTo({
            url: '/pages/index/home/bookinghome/bookinghome?homeId='+this.data.houseId,
          });
        }
      }
    })
  },
  navigateToReport() {
    // 存储数据到本地缓存
    wx.setStorageSync('houseId', this.data.houseId);
    wx.setStorageSync('userId', this.data.userId);
    // 导航到目标页面
    wx.navigateTo({
      url: '/pages/index/home/report/report?type=' + this.data.type
    });
  },
  online() {
    wx.redirectTo({
      url: "/pages/index/home/onlincontact/onlincontact?targetUserId="+this.data.fangdongname,
    })
  },
//收藏按钮
haveSave(e) {
  // console.log(e)
  if (!this.data.isClick == true) {
    let jobData = this.data.jobStorage;
    jobData.push({
      jobid: jobData.length,
      id: this.data.job.id
    })
    wx.setStorageSync('jobData', jobData); //设置缓存
    wx.showToast({
      title: '已收藏',
    });

    wx.request({
      url: 'http://localhost:8080/ihome/collect',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        houseIdOrTopicId: this.data.houseId,
        userId: this.data.userId,
        collectType: this.data.type1,
      },
      method: 'POST',
      success(resp) {
        // console.log(resp);
      }
    });

  } else {
    wx.request({
      url: 'http://localhost:8080/ihome/collect',
      data: {
        houseIdOrTopicId: this.data.houseId,
        userId: this.data.userId,
        collectType: this.data.type1,
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      // data: {
      //   userHouseId: this.data.houseId,
      // },
      method: "DELETE",
      success(resp) {
        // console.log(resp);
      }
    });
    wx.showToast({
      title: '已取消收藏',
    });
  }
  this.setData({
    isClick: !this.data.isClick
  })
},
  getHouse() {
    wx.request({
      url: 'http://localhost:8080/ihome/home/landorder/'+this.data.houseId,
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (resp => {
        this.setData({
          house: resp.data.data
        })
        console.log(resp.data)
      })
    })
  },
  // 房东所有出租房子信息
  getHouseAll() {
    wx.request({
      url: 'http://localhost:8080/ihome/home/house',
      data: {
        userId: this.data.userId
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (resp => {
        // console.log(resp.data)
        this.setData({
          home: resp.data.rows,
          fangdongname:resp.data.rows[0].sysUser.nickName
        })    
      })
    })
  },
  checkCollectionStatus() {
    wx.request({
      url: 'http://localhost:8080/ihome/collect/list',
      data: {
        // JSON.stringify(that.data.myForm)
        // houseId:JSON.stringify(this.data.houseId) ,
        // userId: JSON.stringify(this.data.userId),
        // collectType: JSON.stringify(this.data.type1)

        houseIdOrTopicId: this.data.houseId,
        userId: this.data.userId,
        collectType: this.data.type1
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (resp => {
        // console.log(resp)
        if (resp.data.total != 0) {
          this.setData({
            isClick: true
          });
        } else {
          this.setData({
            isClick: false
          });
        }
      })
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      houseId: options.id,
      // userId: options.userId
    })
    this.checkCollectionStatus(); // 检查收藏状态
    // console.log(this.data.houseId)

    this.getHouse()
    this.getHouseAll()
    wx.getLocation({
      type: 'hl', // 返回可以用于wx.openLocation的经纬度，国测局坐标系，即火星坐标系  
      success: (res => {
        // console.log(res);
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude,
            iconPath: '/images/position.png',
            width: 40,
            height: 40
          }]
        });
      }),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})